package com.lalameow.serverlogingate.service;

import com.lalameow.serverlogingate.enetiy.PlayerInfo;
import com.lalameow.serverlogingate.repository.PlayerInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * 创建人: 陈刚
 * 日期: 2018/5/8
 * 时间: 6:32
 * 功能：请进行修改
 */
@Service
@Slf4j
public class PlayerInfoService {

    @Autowired
    private PlayerInfoRepository playerInfoRepository;

    public boolean hasPlayerName(String playerName){
        return playerInfoRepository.countByPlayerName(playerName)>0;
    }
    public boolean hasUserName(String userName){
        return  playerInfoRepository.countByUserName(userName)>0;
    }
    public PlayerInfo save(PlayerInfo playerInfo){
        return playerInfoRepository.save(playerInfo);
    }
    public PlayerInfo findByUserNameAndPwd(String userName,String pwd){
        return playerInfoRepository.findByUserNameAndPassword(userName, pwd);
    }

    public PlayerInfo updateLoginInfo(String userName,String pwd,String lastLoginIp){
        PlayerInfo playerInfo=this.findByUserNameAndPwd(userName,pwd);
        if(playerInfo!=null){
            playerInfo.setLastLoginIp(lastLoginIp);
            return  playerInfoRepository.save(playerInfo);
        }else {
            return null;
        }
    }

    public PlayerInfo updatePassword(String userName,String oldPwd,String newPwd){
        PlayerInfo playerInfo=this.findByUserNameAndPwd(userName,oldPwd);
        if(playerInfo!=null){
            playerInfo.setPassword(newPwd);
            return  playerInfoRepository.save(playerInfo);
        }else {
            return null;
        }
    }
    public void switchTrust(String playerName){
        PlayerInfo playerInfo=playerInfoRepository.findByPlayerName(playerName);
        if(playerInfo!=null){
            playerInfo.setPassCheat(!playerInfo.isPassCheat());
            playerInfo=playerInfoRepository.save(playerInfo);
            if(playerInfo.isPassCheat()){
                log.info("玩家【{}】账号已经加入信任列表.",playerInfo.getPlayerName());
            }else {
                log.info("玩家【{}】账号已经移除信任列表.",playerInfo.getPlayerName());
            }
        }else {
            log.warn("玩家【{}】信息不存在！.",playerName);
        }
    }
    public void lockedPlayer(String playerName){
        PlayerInfo playerInfo=playerInfoRepository.findByPlayerName(playerName);
        if(playerInfo!=null){
            playerInfo.setLocked(true);
            playerInfoRepository.save(playerInfo);
            log.info("玩家【{}】账号已经被锁定.",playerInfo.getPlayerName());
        }else {
            log.warn("玩家【{}】信息不存在！.",playerName);
        }
    }

    public void unlockedPlayer(String playerName){
        PlayerInfo playerInfo=playerInfoRepository.findByPlayerName(playerName);
        if(playerInfo!=null){
            playerInfo.setLocked(false);
            playerInfoRepository.save(playerInfo);
            log.info("玩家【{}】账号已经被解锁.",playerInfo.getPlayerName());
        }else {
            log.warn("玩家【{}】信息不存在！.",playerName);
        }
    }
}
